/**
 * @name         RepNET View Generator for reporting engine
 * @version      1.0a
 * @author       Kasun Rathnayaka <kazar55@gmail.com>
 * @author       Ramindu Deshapriya <rasade88@gmail.com>
 * @author               Prabodha Dissanayake <prabodha89@gmail.com>
 * @author               Thusira Dissanayake <thusira14@gmail.com>
 * @about        Developed by MiDi
 * @license      http://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License (LGPL)
 * @lastModified 2012.09.24
 */
package com.midi.wholesale.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.JButton;

import com.midi.wholesale.main.PasswordChecker;
import com.midi.wholesale.main.Printer;
//import com.midi.wholesale.main.Printer;
//import com.midi.repnet.main.Printer;
import com.midi.wholesale.main.Reports;
import com.midi.wholesale.models.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import li.netcat.print.util.PagePrint;
import java.awt.Font;

public class ViewGenerator extends JFrame {

	
	private JPanel contentPane;
	public static JFrame final_frame;
	private String cheque_date;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final_frame = new ViewGenerator();
					final_frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public ViewGenerator() {
		super();
	}

	/**
	 * Method to create the frame
	 * 
	 * @param params
	 *            a HashMap containing params needing to be processed when
	 *            generating each view
	 * @param reportType
	 *            One of the contants on report creation
	 * @wbp.parser.constructor
	 */
	public ViewGenerator(String fromDate, String toDate) {
		setResizable(false);
		
			setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			setBounds(100, 100, 664, 416);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			ArrayList<DeliveryResults> list = Reports.getDeliverySummary(fromDate,toDate);
			setUiSummery(list);				
		}
	
	public ViewGenerator(String fromDate, String toDate,Agent agent) {
		    
			
			    setResizable(false);		
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				setBounds(100, 100, 664, 416);
				contentPane = new JPanel();
				contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
				setContentPane(contentPane);
				ArrayList<DeliveryResults> list = Reports.getDeliverySummaryByItems(fromDate,toDate,agent);
				System.out.println("kkk");
				setUiSummeryByItems(list);	
			
		}
	

	public void setUiSummery(ArrayList<DeliveryResults> list) {
		System.out.println("setUiSummery called");
		
		String col[] = { "Date","Lorry","Agent","Route","Item","Sales"};
		final DefaultTableModel model = new DefaultTableModel(null, col) {
			public boolean isCellEditable(int row, int column) {
				return false;// This causes all cells to be not editable
			}
		};
		
		for (DeliveryResults l : list) 										
		{
			model.addRow(new String[] {l.getDate(), l.getLorryId(),l.getAgentId(),l.getRouteId(),l.getItemId(),Integer.toString(l.getIAmount()-l.getRemainder()) });
			
		}
		//System.out.println(model.getRowCount());
		final JTable table = new JTable(model);
		JTableHeader header = table.getTableHeader();
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment( JLabel.RIGHT );
		for(int i=0;i<table.getModel().getColumnCount();i++){
		table.getColumnModel().getColumn(i).setCellRenderer( rightRenderer );
		}
		header.setBackground(Color.yellow);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setFont(new Font("Iskoola Pota", Font.ITALIC, 12));
		contentPane.setLayout(null);
		JScrollPane pane = new JScrollPane(table);
		pane.setEnabled(false);
		pane.setBounds(10, 51, 634, 281);
		contentPane.add(pane);
		
		JLabel lblSalesReport = new JLabel("Sales Report");
		lblSalesReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblSalesReport.setBounds(273, 11, 144, 29);
		contentPane.add(lblSalesReport);

	}
	
	public void setUiSummeryByItems(ArrayList<DeliveryResults> list) {
		System.out.println("setUiSummeryByItems called");
		
		String col[] = { "Item","Sales"};
		final DefaultTableModel model = new DefaultTableModel(null, col) {
			public boolean isCellEditable(int row, int column) {
				return false;// This causes all cells to be not editable
			}
		};
		
		for (DeliveryResults l : list) 										
		{
			model.addRow(new String[] {l.getItemId(),Integer.toString(l.getIAmount())});
			
		}
		//System.out.println(model.getRowCount());
		final JTable table = new JTable(model);
		JTableHeader header = table.getTableHeader();
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment( JLabel.RIGHT );
		for(int i=0;i<table.getModel().getColumnCount();i++){
		table.getColumnModel().getColumn(i).setCellRenderer( rightRenderer );
		}
		header.setBackground(Color.yellow);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setFont(new Font("Iskoola Pota", Font.ITALIC, 12));
		contentPane.setLayout(null);
		JScrollPane pane = new JScrollPane(table);
		pane.setEnabled(false);
		pane.setBounds(10, 51, 634, 281);
		contentPane.add(pane);
		
		JLabel lblSalesReport = new JLabel("Sales Report");
		lblSalesReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblSalesReport.setBounds(273, 11, 144, 29);
		contentPane.add(lblSalesReport);

	}
}